'''
Created on Jan 27, 2014

@author: dmen
'''
import threading
import datetime
import time
from com.ad.sword.MonitorTask import Task
from com.ad.sword.service import SuperviseeService

class SchedTimer(threading.Thread):
    def __init__(self,interval):
        self.interval=interval
        threading.Thread.__init__(self,name="Scheduler Timer %d"%interval)
    def run(self):
        while True:
            now = datetime.datetime.now()
            print "%s  SchedTimer at time: %s   interval %d" % (self.getName(), now,self.interval)
            tasks=self.createTasks()
            # TODO use thread pool
            for t in tasks:
                t.start()
            time.sleep(self.interval*60)
    def createTasks(self):
        supervisees=SuperviseeService.list(self.interval)
        tasks=[]
        for s in supervisees:
            t=Task(s)
            tasks.append(t)
        return tasks

    
